package pattern.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pattern.WorkServiceTool;
import pattern.annontations.Menu;
import pattern.annontations.Secure;
import pattern.dto.UserLog;
import pattern.dto.model.system.LoginLogModel;
import pattern.dto.response.system.LoginLogResponse;
import pattern.enums.dict.common.YesOrNotEnum;
import pattern.model.BaseIdModel;
import pattern.result.ResponseData;
import pattern.result.ResponsePage;
import pattern.result.ResponseResultStatus;
import pattern.routing.WorkMainApiRouting;
import pattern.utils.CollectionUtil;

import java.util.Arrays;

/**
 * @author Simon
 * @date 2021-05-10
 */
@Api(tags = "用户登陆日志")
@RestController
@RequestMapping(value = WorkMainApiRouting.LoginLog.BASE)
@Menu(menuName = "login_log", menuUri = "login_log", foundational = false)
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Secure
@Slf4j
public class UserLogController {

    @ApiOperation(value = "查询")
    @GetMapping(WorkMainApiRouting.LoginLog.LIST)
    @Secure(actionUri = "login_log:search", actionName = "search", actionNote = "login_log")
    public ResponsePage<LoginLogResponse> search(LoginLogModel model) {
        var responsePage = WorkServiceTool.getUserLogService().pageResponse(model, LoginLogResponse.class);
        if (CollectionUtil.isNotEmpty(responsePage.getRecords())) {
            for (LoginLogResponse record : responsePage.getRecords()) {
                var get = WorkServiceTool.getCache().userCache.GET(record.getToken());
                record.setStatus(get.isSuccess()? YesOrNotEnum.YES.getCode():YesOrNotEnum.NO.getCode());
            }
        }
        return responsePage;
    }

    @ApiOperation(value = "详情")
    @GetMapping(WorkMainApiRouting.LoginLog.DETAIL)
    @Secure(actionUri = "login_log:search")
    public ResponseData<UserLog> detail(@PathVariable Integer id) {
        return new ResponseData<>(WorkServiceTool.getUserLogService().getById(new BaseIdModel<>(id)));
    }

    @ApiOperation(value = "删除")
    @DeleteMapping(WorkMainApiRouting.LoginLog.DELETE)
    @Secure(actionUri = "login_log:remove", actionName = "delete", actionNote = "login_log")
    public ResponseData<Boolean> delete(@PathVariable Integer[] id) {
        return new ResponseData<>(WorkServiceTool.getUserLogService().removeByIds(new BaseIdModel<>(Arrays.asList(id.clone()))));
    }

    @ApiOperation(value = "清除")
    @DeleteMapping(WorkMainApiRouting.LoginLog.CLEAR)
    @Secure(actionUri = "login_log:remove_all", actionName = "delete_all", actionNote = "login_log")
    public ResponseData<Boolean> clear() {
        //todo
        return new ResponseData<>(ResponseResultStatus.NO_ACCESS);
//        return new ResponseData<>(WorkServiceTool.getUserLogService().removeAll(new BaseIdModel<>()));
    }
}
